// 封装的 Moonshot API 请求函数
import {VITE_MOONSHOT_API_KEY} from "@/config.js";

async function fetchMoonshotCompletion(messages) {
    const response = await fetch('https://api.moonshot.cn/v1/chat/completions', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            // 将你的API密钥放置于环境变量中，并在这里引用
            'Authorization': `Bearer ${VITE_MOONSHOT_API_KEY}`
        },
        body: JSON.stringify({
            model: "moonshot-v1-8k",
            messages: messages,
            temperature: 0.3
        })
    });

    if (!response.ok) {
        const message = `有一个错误产生: ${response.status}`;
        throw new Error(message);
    }
    return await response.json();
}

export default fetchMoonshotCompletion;